﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmUnitedCheckOut : BaseForm
    {
        public OrderInfo orderInfo = null;

        public FrmUnitedCheckOut()
        {
            InitializeComponent();
        }

        private void FrmUnitedCheckOut_Load(object sender, EventArgs e)
        {
            if (orderInfo != null)
            {
                if (orderInfo.DepositMoney > 0)
                {
                    ListViewItem item = new ListViewItem(orderInfo.PayType);
                    item.SubItems.Add(orderInfo.DepositMoney.ToString("C2"));
                    item.SubItems.Add(orderInfo.CreateDate.ToString());
                    item.SubItems.Add(orderInfo.Createor);

                    this.listView1.Items.Add(item);
                }
            }
        }

        #region 选择对应支付方式

        private void chkCash_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkControl = (CheckBox)sender;
            this.txtCashPay.Enabled = chkControl.Checked;

            if (chkControl.Checked)
            {
                decimal amount = Convert.ToDecimal(this.lblAmount.Text);
                decimal pay = GetAllPayMoney();
                this.txtCashPay.Text = (amount - pay).ToString("f2");
            }
            else
            {
                this.txtCashPay.Text = "0.00";
            }
        }

        private void chkDaijin_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkControl = (CheckBox)sender;
            this.txtDaijinTicket.Enabled = chkControl.Checked;
            if (chkControl.Checked)
            {
                decimal amount = Convert.ToDecimal(this.lblAmount.Text);
                decimal pay = GetAllPayMoney();
                this.txtDaijinTicket.Text = (amount - pay).ToString("f2");
            }
            else
            {
                this.txtDaijinTicket.Text = "0.00";
            }
        }

        private void chkNational_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkControl = (CheckBox)sender;
            this.txtNationalCredit.Enabled = chkControl.Checked;
            this.txtNationalCreditCard.Enabled = chkControl.Checked;
            if (chkControl.Checked)
            {
                decimal amount = Convert.ToDecimal(this.lblAmount.Text);
                decimal pay = GetAllPayMoney();
                this.txtNationalCredit.Text = (amount - pay).ToString("f2");
            }
            else
            {
                this.txtNationalCredit.Text = "0.00";
            }
        }

        private void chkInterCredit_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkControl = (CheckBox)sender;
            this.txtInterCredit.Enabled = chkControl.Checked;
            this.txtInterCreditCard.Enabled = chkControl.Checked;
            if (chkControl.Checked)
            {
                decimal amount = Convert.ToDecimal(this.lblAmount.Text);
                decimal pay = GetAllPayMoney();
                this.txtInterCredit.Text = (amount - pay).ToString("f2");
            }
            else
            {
                this.txtInterCredit.Text = "0.00";
            }
        }

        private void chkCard_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkControl = (CheckBox)sender;
            this.txtCard.Enabled = chkControl.Checked;
            this.txtCardNumber.Enabled = chkControl.Checked;
            if (chkControl.Checked)
            {
                decimal amount = Convert.ToDecimal(this.lblAmount.Text);
                decimal pay = GetAllPayMoney();
                this.txtCard.Text = (amount - pay).ToString("f2");
            }
            else
            {
                this.txtCard.Text = "0.00";
            }
        } 

        #endregion

        #region 支付金额变化的时候，更新还应付和找零数字
        private void RefreshChange()
        {
            decimal amount = Convert.ToDecimal(this.lblAmount.Text);
            decimal deposit = Convert.ToDecimal(this.lblDeposit.Text);
            decimal pay = GetAllPayMoney();
            if ((amount - pay) >= 0)
            {
                this.lblYinfu.Text = (amount - pay).ToString("f2");
            }
            else
            {
                this.lblYinfu.Text = "0.00";
            }
            this.lblChange.Text = (pay + deposit - amount).ToString("f2");
        }

        private void txtCashPay_TextChanged(object sender, EventArgs e)
        {
            RefreshChange();
        }

        private void txtDaijinTicket_TextChanged(object sender, EventArgs e)
        {
            RefreshChange();
        }

        private void txtNationalCredit_TextChanged(object sender, EventArgs e)
        {
            RefreshChange();
        }

        private void txtInterCredit_TextChanged(object sender, EventArgs e)
        {
            RefreshChange();
        }

        private void txtCard_TextChanged(object sender, EventArgs e)
        {
            RefreshChange();
        } 
        #endregion

        private decimal GetAllPayMoney()
        {
            decimal cashMoney = Convert.ToDecimal(this.txtCashPay.Text);
            decimal daijinMoney = Convert.ToDecimal(this.txtDaijinTicket.Text);
            decimal nationalMoney = Convert.ToDecimal(this.txtNationalCredit.Text);
            decimal interMoney = Convert.ToDecimal(this.txtInterCredit.Text);
            decimal cardMoney = Convert.ToDecimal(this.txtCard.Text);

            decimal pay = cashMoney + daijinMoney + nationalMoney + interMoney + cardMoney;

            return pay;
        }

        private string GetDefaultNote()
        {
            string note = this.txtNote.Text;
            if (this.chkCash.Checked)
            {
                note += string.Format(";现金:{0}", this.txtCashPay.Text);
            }
            if (this.chkDaijin.Checked)
            {
                note += string.Format(";代金劵:{0}", this.txtDaijinTicket.Text);
            }
            if (this.chkNational.Checked)
            {
                note += string.Format(";国内信用卡:{0}", this.txtNationalCredit.Text);
            }
            if (this.chkInterCredit.Checked)
            {
                note += string.Format(";国际信用卡:{0}", this.txtInterCredit.Text);
            } 
            if (this.chkCard.Checked)
            {
                note += string.Format(";储值卡:{0}", this.txtCard.Text);
            }

            return note;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 验证卡号输入
            if (this.chkNational.Checked)
            {
                if (this.txtNationalCreditCard.Text.Length == 0)
                {
                    MessageUtil.ShowTips("请输入卡号");
                    txtNationalCreditCard.Focus();
                    return;
                }
            }
            else if (this.chkInterCredit.Checked)
            {
                if (this.txtInterCreditCard.Text.Length == 0)
                {
                    MessageUtil.ShowTips("请输入卡号");
                    txtInterCreditCard.Focus();
                    return;
                }
            }
            else if (this.chkCard.Checked)
            {
                if (this.txtCardNumber.Text.Length == 0)
                {
                    MessageUtil.ShowTips("请输入卡号");
                    txtCardNumber.Focus();
                    return;
                }
            } 
            #endregion

            decimal amount = Convert.ToDecimal(this.lblAmount.Text);
            decimal pay = GetAllPayMoney();
            if (pay < amount)
            {
                MessageUtil.ShowTips("付款金额不足，不能进行结账");
                return;
            }

            if (MessageUtil.ShowYesNoAndTips("您是否确定结账？") != DialogResult.Yes)
                return;

            this.txtNote.Text = GetDefaultNote();//
            this.DialogResult = DialogResult.OK;
        }

    }
}
